package com.glory.hzlionpda.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.glory.hzlionpda.R;
import com.glory.hzlionpda.base.BasePollingActivity;
import com.glory.hzlionpda.base.Constant;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingActivity extends BasePollingActivity {


    @BindView(R.id.ivIp)
    ImageView ivIp;
    @BindView(R.id.tvIP)
    TextView tvIP;
    @BindView(R.id.rlIP)
    RelativeLayout rlIP;
    @BindView(R.id.tvVersion)
    TextView tvVersion;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvFileIP)
    TextView tvFileIP;
    @BindView(R.id.tvTimeOut)
    TextView tvTimeOut;
    @BindView(R.id.rlTimeOut)
    RelativeLayout rlTimeOut;

    private FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog;

    @OnClick({R.id.rlIP, R.id.rlFileIP,R.id.rlTimeOut})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlIP:
                freeUI_generalFragmentDialog = getDialog(Constant.REQUEST_URL, s -> {
                    freeUI_generalFragmentDialog.dismiss();
                    Constant.REQUEST_URL = s;
                    tvIP.setText(Constant.REQUEST_URL);
                    FreeApi_SharePreferencesUtils.setSharePre("URL_FILE", "URL_IP", s, getApplicationContext());
                    showToast("保存成功", true);
                });
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "URL_IP_DIALOG");
                break;
            case R.id.rlFileIP:
                freeUI_generalFragmentDialog = getDialog(Constant.FILE_URL, s -> {
                        freeUI_generalFragmentDialog.dismiss();
                        Constant.FILE_URL = s;
                        tvFileIP.setText(Constant.FILE_URL);
                        FreeApi_SharePreferencesUtils.setSharePre("URL_FILE", "FILE_IP", s, getApplicationContext());
                        showToast("保存成功", true);
                });
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "FILE_IP_DIALOG");
                break;
            case R.id.rlTimeOut:
                freeUI_generalFragmentDialog = FreeApi_DialogUtils.getScanDialog(this, "请输入超时时间", Constant.DEFAULT_TIME_OUT, new FreeUI_TextCallback() {
                    @Override
                    public void onSuccess(String s) {
                        freeUI_generalFragmentDialog.dismiss();
                        Constant.DEFAULT_TIME_OUT = s;
                        tvTimeOut.setText(Constant.DEFAULT_TIME_OUT+"s");
                        FreeApi_SharePreferencesUtils.setSharePre("TIME_OUT_FILE", "TIME_OUT", s, getApplicationContext());
                        showToast("保存成功，请重启APP生效", true);
                        rlTimeOut.setVisibility(View.GONE);
                    }
                });
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "TIME_OUT_DIALOG");
                break;
        }
    }

    private FreeUI_GeneralFragmentDialog getDialog(String ip,FreeUI_TextCallback editTextScanCallback){
        final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_address, null);
        TextView tvConfirm = view.findViewById(R.id.tvConfirm);
        TextView tvCancel = view.findViewById(R.id.tvCancel);
        final FreeUI_ClearEditText etIp = view.findViewById(R.id.etIp);
//        etIp.setText(ip);
        etIp.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count <= 1) {
                    etIp.setText("");
                }

                if (start == 0 && before == 0 && count > 1) {
                    editTextScanCallback.onSuccess(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        tvConfirm.setOnClickListener(view12 -> {
            if (!etIp.getText().toString().isEmpty()){
                editTextScanCallback.onSuccess(etIp.getText().toString().trim());
            }
        });
        tvCancel.setOnClickListener(view1 -> freeUI_generalFragmentDialog.dismiss());
        freeUI_generalFragmentDialog.setLayout(view);
       return freeUI_generalFragmentDialog;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("设置");
    }

    @Override
    protected void initData() {
        tvVersion.setText(FreeApi_Utils.getVerName());
        tvIP.setText(Constant.REQUEST_URL);
        tvFileIP.setText(Constant.FILE_URL);
        tvTimeOut.setText(Constant.DEFAULT_TIME_OUT+"s");
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }
}

